package com.hpradip.android.accessNumberSpeedDialer;

import com.google.android.apps.analytics.GoogleAnalyticsTracker;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;

public class DialpadActivity extends Activity {

	private String[] numbers;
	private TextView numTxt;
	private SharedPreferences preferences;
	private GoogleAnalyticsTracker tracker;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		tracker = GoogleAnalyticsTracker.getInstance();
		tracker.setAnonymizeIp(true);
		tracker.startNewSession(
				getResources().getString(R.string.analytics_tracking_code),
				this);

		setContentView(R.layout.dialpad);

		GridView gridView = (GridView) findViewById(R.id.grid_view);
		numTxt = (TextView) findViewById(R.id.dial_number_text);
		numTxt.setText("");

		ImageButton backButton = (ImageButton) findViewById(R.id.back_button);
		backButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (numTxt.getText().length() == 0)
					return;
				numTxt.setText(numTxt.getText().subSequence(0,
						numTxt.getText().length() - 1));
			}
		});

		backButton.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
				numTxt.setText("");
				return true;
			}
		});

		preferences = PreferenceManager.getDefaultSharedPreferences(this);

		ImageButton callButton = (ImageButton) findViewById(R.id.call_button);
		callButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Resources resources = getResources();
				AccessNumberSpeedDialerActivity.dialNumber(
						DialpadActivity.this, 0, numTxt.getText().toString(),
						tracker);
			}
		});

		numbers = new String[12];
		for (Integer i = 1; i <= 9; i++) {
			numbers[i - 1] = i.toString();
		}

		numbers[9] = "+";
		numbers[10] = "0";
		numbers[11] = "#";

		gridView.setAdapter(new NumbersAdapter(this, R.layout.number_list_item,
				numbers));
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				numTxt.append(numbers[position]);
			}
		});

		ImageButton settings_btn = (ImageButton) findViewById(R.id.menu_settings);
		settings_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				AccessNumberSpeedDialerActivity
						.showPreferences(DialpadActivity.this);
			}
		});

		ImageButton help_btn = (ImageButton) findViewById(R.id.menu_help);
		help_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				AccessNumberSpeedDialerActivity.showHelp(DialpadActivity.this);
			}
		});
	}

	@Override
	protected void onResume() {
		tracker.trackPageView("/" + this.getClass().getSimpleName());
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		tracker.dispatch();
	}
}
